using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SQLDAL;
using System.Data.SqlClient;

/// <summary>
/// Summary description for UserBll
/// All method for User Table
/// By: pvhnhan
/// Date: 4/5/2009
public class UserBll
{
	public UserBll()
    {
        
    }
    #region List all user
    public SqlDataReader ListAllUser()
    {
        return SQLHelper.ExecuteReader(SQLHelper.CONN_STRING,
            CommandType.StoredProcedure, "sp_User_List", null);
    }
    #endregion

    #region Create new user
    public int CreateUser(UserInfo userInfo)
    {
        SqlParameter[] cmdParms =
            {
                new SqlParameter("@userID", SqlDbType.VarChar, 10),
                new SqlParameter("@password", SqlDbType.VarChar, 30),
                new SqlParameter("@roleID", SqlDbType.Int, 4)
            };
        cmdParms[0].Value = userInfo.UserID;
        cmdParms[1].Value = userInfo.Password;
        cmdParms[2].Value = userInfo.RoleID;

        int result = SQLHelper.ExecuteNonQuery(SQLHelper.CONN_STRING, 
            CommandType.StoredProcedure, "sp_User_Create", cmdParms);
        
        return result;
    }
    #endregion

    #region Update User's Information
    public int UpdateUser(UserInfo userInfo)
    {
        SqlParameter[] cmdParms =
            {
                new SqlParameter("@userID", SqlDbType.VarChar, 10),
                new SqlParameter("@password", SqlDbType.VarChar, 30),
               // new SqlParameter("@roleID", SqlDbType.Int, 4)
            };
        cmdParms[0].Value = userInfo.UserID;
        cmdParms[1].Value = userInfo.Password;
       // cmdParms[2].Value = userInfo.RoleID;

        int result = SQLHelper.ExecuteNonQuery(SQLHelper.CONN_STRING,
            CommandType.StoredProcedure, "sp_User_Update", cmdParms);

        return result;
    }
    #endregion

    #region Delete User
    public int DeleteUser(UserInfo userInfo)
    {
        SqlParameter[] cmdParms =
            {
                new SqlParameter("@userID", SqlDbType.VarChar, 10)
            };
        cmdParms[0].Value = userInfo.UserID;

        int result = SQLHelper.ExecuteNonQuery(SQLHelper.CONN_STRING,
            CommandType.StoredProcedure, "sp_User_Delete", cmdParms);
        return result;
    }
    #endregion

    #region Check login
    public object CheckLogin(UserInfo userInfo)
    {
        SqlParameter[] cmdParms =
            {
                new SqlParameter("@userID", SqlDbType.VarChar, 10),
                new SqlParameter("@password", SqlDbType.VarChar, 30),
                new SqlParameter("@roleID", SqlDbType.Int, 4)
            };
        cmdParms[0].Value = userInfo.UserID;
        cmdParms[1].Value = userInfo.Password;
        cmdParms[2].Value = userInfo.RoleID;

        object result = SQLHelper.ExecuteScalar(SQLHelper.CONN_STRING,
            CommandType.StoredProcedure, "sp_User_CheckLogin", cmdParms);

        return result;
    }
    #endregion

    #region
    public int CheckUserPass(UserInfo userInfo)
    {
        SqlParameter[] cmdParms =
            {
                new SqlParameter("@userID", SqlDbType.VarChar, 10),
                new SqlParameter("@password", SqlDbType.VarChar, 30),
            };
        cmdParms[0].Value = userInfo.UserID;
        cmdParms[1].Value = userInfo.Password;

        int result = (int)SQLHelper.ExecuteScalar(SQLHelper.CONN_STRING,
            CommandType.StoredProcedure, "sp_User_CheckIDPass", cmdParms);

        return result;
    }
    #endregion
}
